﻿Option Explicit On
Imports System.Text.RegularExpressions

Public Module Functions
    Public Function toTime(ByVal tick As Long, Optional ByVal ore As Boolean = True) As String
        Dim h As Integer
        Dim m As Integer
        Dim s As Integer

        h = Math.Floor(tick / 3600)
        tick -= h * 3600
        m = Math.Floor(tick / 60)
        tick -= m * 60
        s = tick
        If (ore) Then
            toTime = h & ":" & Format(m, "0#") & ":" & Format(s, "0#")
        Else
            toTime = m & ":" & Format(s, "0#")
        End If
    End Function

    Public Function pausaSingle(ByVal s As String) As Single
        Dim sec As Integer = 0
        Dim min As Integer = 0
        Dim rsec As New Regex("([\d]*)''")
        Dim rmin As New Regex("([\d]*)' *")
        'Dim r As New Regex("(([\d]*)'.*)*(([\d]*)'')*")
        Dim m As Match

        'm = r.Match(s)

        m = rsec.Match(s)
        If m.Success Then sec = m.Groups(1).Value
        m = rmin.Match(s)
        If m.Success Then min = m.Groups(1).Value
        pausaSingle = min + sec / 60
    End Function

    Public Function caricoVal(ByVal s As String) As String
        Dim r As New Regex("([\d]*).*(kg)*")
        Dim m As Match
        m = r.Match(s)
        caricoVal = "null"
        If m.Success Then caricoVal = m.Groups(1).Value
        If caricoVal = "" Then caricoVal = "null"
    End Function

    Public Function ripetizioni(ByVal s As String, ByRef serie As Integer, ByRef ripe As Integer) As Integer
        Dim r As New Regex("([\d]*) *[xX] *([\d]*)")
        Dim m As Match
        m = r.Match(s)
        ripetizioni = 0
        If m.Success And m.Groups.Count > 2 Then
            If m.Groups(1).Value <> "" And m.Groups(2).Value <> "" Then
                serie = m.Groups(1).Value
                ripe = m.Groups(2).Value
                ripetizioni = serie * ripe
            End If
        End If
    End Function

    Public Function pausaLong(ByVal val As Single) As Long
        Dim int As Integer = Math.Floor(val)
        Dim dec As Single = (val - int) * 60
        pausaLong = int * 60 + dec
    End Function

    Public Function convPausa(ByVal val As Single) As String
        Dim int As Integer = Math.Floor(val)
        Dim dec As Integer = (val - int) * 60
        convPausa = ""
        If int > 0 Then convPausa = int & "'"
        If (dec > 0) Then convPausa &= dec & "''"
    End Function

    Public Function capitalizza(ByVal s As String) As String
        Dim token() As String
        token = s.Split(" ")
        capitalizza = ""
        For Each o As String In token
            capitalizza &= Char.ToUpper(o(0)) & Mid(o, 2) & " "
        Next
        capitalizza = capitalizza.Remove(capitalizza.Length - 1)
    End Function
End Module
